
function ObjectModel(values, parent) {

    this.__parent = parent || null;
    this.__fields = $create(this.__defaults);
    this.__subs = null;
    this.__deps = null;

    if (values)
    {
        for (var name in values)
        {
            this[name] = values[name];
        }
    }
}


yaxi.ObjectModel = Object.extend(function (base) {



    // 扩展属性集支持
    extendProperties(ObjectModel, function (properties) {

        var prototype = this.prototype;
        var descriptor;

        // 生成属性集
        for (var name in properties)
        {
            if (descriptor = properties[name])
            {
                switch (typeof descriptor)
                {
                    case 'object':
                        break;

                    case 'function':
                        // submodel
                        if (descriptor.$Submodel)
                        {
                            descriptor = descriptor.$Submodel(name, descriptor);
                        }
                        else // readonly property
                        {
                            descriptor = { defaultValue: null, get: descriptor };
                        }
                        break;

                    default:
                        descriptor = { defaultValue: descriptor };
                        break;
                }
            }
            else
            {
                descriptor = { defaultValue: descriptor == null ? null : descriptor };
            }

            defineProperty(prototype, name, descriptor);
        }
    });



    // 作为子模型属性
    function $Submodel(name) {

        var Class = this;

        return {

            get: function () {

                var subs = this.__subs || (this.__subs = {});
                return subs[name] || (subs[name] = new Class(null, this));
            },

            set: function (value) {

                var subs = this.__subs;
                var instance = subs && subs[name];

                if (value)
                {
                    if (instance)
                    {
                        for (var key in value)
                        {
                            instance[key] = value[key];
                        }
                    }
                    else
                    {
                        (subs || (this.__subs = {}))[name] = new Class(value, this);
                    }
                }
                else if (instance)
                {
                    Class.clear(instance);
                }
            }
        };
    }


    // 从数组对象创建对象模型数组
    function fromArray(items) {

        if (items)
        {
            var length = items.length;
            var result = new Array(length);

            for (var i = 0; i < length; i++)
            {
                result[i] = new this(items[i]);
            }

            return result;
        }

        return [];
    }


    // 获取模型对象的根对象
    function root(instance) {

        var parent;

        while (instance)
        {
            if (parent = instance.__parent)
            {
                instance = parent;
            }
        }

        return instance;
    }


    // 获取模型对象的父对象
    function parent(instance) {

        return instance && instance.__parent;
    }


    // 复制对象模型
    function copy(instance) {

        if (instance)
        {
            var model = new this();
            var source = model.__fields;
            var target = instance.__fields;

            for (var name in target)
            {
                source[name] = target[name];
            }

            return model;
        }
    }


    // 清除复制对象属性
    function clear(instance) {

        if (instance)
        {
            var subs = instance.__subs;

            for (var name in subs)
            {
                subs[name].__destroy();
                subs[name] = null;
            }
            
            return instance;
        }
    }


    // 销毁模型对象
    function destroy(instance) {

        if (instance)
        {
            instance.__destroy();
            return instance;
        }
    }



    // 初始化类
    (function init(Class) {

        Class.init = init;

        Class.$Submodel = $Submodel;
        Class.fromArray = fromArray;
        Class.root = root;
        Class.parent = parent;
        Class.copy = copy;
        Class.clear = clear;
        Class.destroy = destroy;

    })(ObjectModel);



    // 内部销毁方法
    this.__destroy = function () {

        var subs = this.__subs;

        for (var name in subs)
        {
            subs[name].__destroy();
            subs[name] = null;
        }

        this.__parent = null;
        removeDependences(this);
    }



}, ObjectModel);
